package com.smarthealthring.mm.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作本地数据库
 *
 * Created by Administrator on 2016/6/17.
 */
public class LocalDbUtils {


    /***
     * 复制db文件
     */
//    private void copyLocalDB() {
//        new Thread(() -> {
//            File file = new File("/data/data/" + getPackageName() + "/databases");
//            File dbfile = new File("/data/data/" + getPackageName() + "/databases/" + getText(R.string.db_city_name).toString());
//            File db_old = new File("/data/data/" + getPackageName() + "/databases/" + getText(R.string.db_city_name_old).toString());
//            if (db_old.exists()) {
//                db_old.delete();
//            }
//            if (!dbfile.exists()) {
//                file.mkdirs();
//                copyFile(dbfile);
//            }
//        }).start();
//    }
//
//    private void copyFile(File dbfile) {
//        AssetManager assets = HomeActivity.this.getAssets();
//        InputStream is = null;
//        try {
//            is = assets.open(getText(R.string.db_city_name).toString());
//            int len = 0;
//            byte buffer[] = new byte[1024];
//            FileOutputStream fos = new FileOutputStream(dbfile.getAbsolutePath());
//            while ((len = is.read(buffer)) != -1) {
//                fos.write(buffer, 0, len);
//            }
//            fos.flush();
//            fos.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    /**
     * tabview获取区域
     *
     * @param context
     * @return
     */
    public static List<TabArea> getDistList(Context context) {
        List<TabArea> tabAreaList = new ArrayList<>();
        TabArea tabArea = new TabArea();
        tabArea.setDistrict_name("不限");
        tabAreaList.add(tabArea);
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/data/data/" + context.getPackageName() + "/databases/" + "city.db", null);
        Cursor cursor = db.rawQuery("select * from Tab_District where district_name!='其他' ", null);
        while (cursor.moveToNext()) {
            tabArea = new TabArea();
            //city_code	district_code	district_name	district_abbr	createtime
            String cityCode = cursor.getString(cursor.getColumnIndex("city_code"));
            String districtCode = cursor.getString(cursor.getColumnIndex("district_code"));
            String districtName = cursor.getString(cursor.getColumnIndex("district_name"));
            String districtAbbr = cursor.getString(cursor.getColumnIndex("district_abbr"));

            tabArea.setCity_code(cityCode);
            tabArea.setDistrict_code(districtCode);
            tabArea.setDistrict_name(districtName);
            tabArea.setDistrict_abbr(districtAbbr);

            tabAreaList.add(tabArea);
        }
        cursor.close();
        db.close();
        return tabAreaList;
    }

    /**
     * tabview点击区域获取片区
     *
     * @param context
     * @return
     */
    public static List<TabArea> getAreaList(Context context, String code) {
        List<TabArea> tabAreaList = new ArrayList<>();
        TabArea tabArea = new TabArea();
        tabArea.setArea_name("不限");
        tabArea.setDistrict_code(code);
        tabAreaList.add(tabArea);
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/data/data/" + context.getPackageName() + "/databases/" + "city.db", null);
        Cursor cursor = db.rawQuery("select * from Tab_Area where dist_code = ? ", new String[]{code});
        while (cursor.moveToNext()) {
            tabArea = new TabArea();
            //city_code	dist_code	area_code	area_name
            String cityCode = cursor.getString(cursor.getColumnIndex("city_code"));
            String distCode = cursor.getString(cursor.getColumnIndex("dist_code"));
            String areaCode = cursor.getString(cursor.getColumnIndex("area_code"));
            String areaName = cursor.getString(cursor.getColumnIndex("area_name"));

            tabArea.setCity_code(cityCode);
            tabArea.setDistrict_code(distCode);
            tabArea.setArea_code(areaCode);
            tabArea.setArea_name(areaName);

            tabAreaList.add(tabArea);
        }
        cursor.close();
        db.close();
        return tabAreaList;
    }
}
